eval '(exit $?0)' && eval 'exec perl -wS "$0" ${1+"$@"}'
  & eval 'exec perl -wS "$0" $argv:q'
    if 0;
# Determine whether two files have the same extents by comparing
# the logical block numbers and lengths from filefrag -v for each.

# Invoke like this:
# This helper function, f, extracts logical block number and lengths.
# f() { awk '/^ *[0-9]/ {printf "%d %d ",$2,NF<5?$NF:$5} END {print ""}'; }
# { filefrag -v j1 | f; filefrag -v j2 | f; } | ./filefrag-extent-compare

use warnings;
use strict;
(my $ME = $0) =~ s|.*/||;

my @line = <>;
my $n_lines = @line;
$n_lines == 2
  or die "$ME: expected exactly two input lines; got $n_lines\n";

my @A = split ' ', $line[0];
my @B = split ' ', $line[1];
@A % 2 || @B % 2
  and die "$ME: unexpected input: odd number of numbers; expected even\n";

my @a;
my @b;
foreach my $i (0..@A/2-1) { $a[$i] = { L_BLK => $A[2*$i], LEN => $A[2*$i+1] } };
foreach my $i (0..@B/2-1) { $b[$i] = { L_BLK => $B[2*$i], LEN => $B[2*$i+1] } };

my $i = 0;
my $j = 0;
while (1)
  {
    !defined $a[$i] && !defined $b[$j]
      and exit 0;
    defined $a[$i] && defined $b[$j]
      or die "\@a and \@b have different lengths, even after adjustment\n";
    ($a[$i]->{L_BLK} == $b[$j]->{L_BLK}
     && $a[$i]->{LEN} == $b[$j]->{LEN})
      and next;
    ($a[$i]->{LEN} < $b[$j]->{LEN}
     && exists $a[$i+1] && $a[$i]->{LEN} + $a[$i+1]->{LEN} == $b[$j]->{LEN})
      and ++$i, next;
    exists $b[$j+1] && $a[$i]->{LEN} == $b[$i]->{LEN} + $b[$i+1]->{LEN}
      and ++$j, next;
    die "differing extent:\n"
      . "  [$i]=$a[$i]->{L_BLK} $a[$i]->{LEN}\n"
      . "  [$j]=$b[$j]->{L_BLK} $b[$j]->{LEN}\n"
  }
continue
  {
    ++$i;
    ++$j;
  }

### Setup "GNU" style for perl-mode and cperl-mode.
## Local Variables:
## mode: perl
## perl-indent-level: 2
## perl-continued-statement-offset: 2
## perl-continued-brace-offset: 0
## perl-brace-offset: 0
## perl-brace-imaginary-offset: 0
## perl-label-offset: -2
## perl-extra-newline-before-brace: t
## perl-merge-trailing-else: nil
## End:
